package com.task.poker.card.services;

import com.alibaba.fastjson.JSON;
import com.task.poker.card.entity.*;
import org.json.JSONObject;
import com.task.poker.card.container.CardContainer;
import com.task.poker.card.abs.CardType;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * PockerDeck class.
 * @author	JiDong
 * @create	November 14.2019
 *
 */
public class PockerDeck {

    public static volatile boolean stop = false;
    private Lock locker = new ReentrantLock();
    // sernder
    private Condition sender = locker.newCondition();
    // receiver
    private Condition receiver = locker.newCondition();
    private Map<String,String> playerMap = new HashMap<String, String>(3);//p1 p2 p3

    private String cardArray = "{\"A\"=1,\"2\"=2,\"3\"=3,\"4\"=4,\"5\"=5,\"6\"=6,\"7\"=7,\"8\"=8,\"9\"=9,\"10\"=10,\"J\"=11,\"Q\"=12,\"K\"=13,\"black Joke\"=20,\"red Joke\"=20}";
    private List<String> cards= new ArrayList<>(54);
    private Map<String, Object> cardMap = new ConcurrentHashMap<>();

    public List<String> initializeCard() {
        cardArray = cardArray.replace("=",":");
       JSONObject jsonObject = new JSONObject(cardArray);
       Iterator iterator = jsonObject.keySet().iterator();
       CardContainer container = CardContainer.newInstance();
       int size = jsonObject.keySet().size();
       while(size-- > 0){
           String figure = iterator.next().toString();
           if (figure.equals("black Joke") || figure.equals("red Joke")) {
               JokeCard jokeCard = (JokeCard) container.getCard(CardType.JOKE.getCode(), figure);
               cards.add(jokeCard.getFigure());
               continue;
           }

           HeartCard heart = (HeartCard) container.getCard(CardType.HEART.getCode(), figure);
           DiamondCard diamond = (DiamondCard) container.getCard(CardType.DIAMOND.getCode(), figure);
           SpadeCard spade = (SpadeCard) container.getCard(CardType.SPADE.getCode(), figure);
           ClubCard club = (ClubCard) container.getCard(CardType.CLUB.getCode(), figure);

           cards.add(heart.getFigure());
           cards.add(diamond.getFigure());
           cards.add(spade.getFigure());
           cards.add(club.getFigure());
       }

       this.shuffleCard();
        cardMap = jsonObject.toMap();
       return cards;
    }

    /**
     * shuffle card
     */
    public void shuffleCard() {
        Collections.shuffle(cards);
    }


    /**
     * shuffle card
     */
    public void display() {
        System.out.println(JSON.toJSONString(cards));
    }

    public Lock getLocker() {
        return locker;
    }

    public Condition getSender() {
        return sender;
    }

    public Condition getReceiver() {
        return receiver;
    }


    public Map<String, String> getPlayerMap() {
        return playerMap;
    }

    public int getFigure(String key) {
        return (int) cardMap.get(key);
    }
}
